package com.interauto.service.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.interauto.Dao.Mapper.MgCaseRuntInter;
import com.interauto.Dao.Mapper.MgInteruntInter;
import com.interauto.Dao.Mapper.MgRunTaskInter;
import com.interauto.Dao.entity.Caserunt;
import com.interauto.Dao.entity.Interunt;
import com.interauto.Dao.entity.Runtasktable;
import com.interauto.bean.httpinfo.HttpResPonseBean;
import com.interauto.bean.web.*;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.safety.Whitelist;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 处理任务相关的web业务
 */

@Component
public class RunTaskPageService {
    private static final int pagesums=20;
    private static final Logger logger= LoggerFactory.getLogger(RunTaskPageService.class);
    @Autowired
    private MgRunTaskInter mgRunTaskInter;

    @Autowired
    private MgCaseRuntInter mgCaseRuntInter;
    @Autowired
    private MgInteruntInter mgInteruntInter;


    /**
     * 通过任务ID，获取任务详情
     * @param taskid 要查询的任务ID
     * @return 任务信息
     */
    public RunTaskWeb getRunTaskWeb(int taskid){
        RunTaskWeb runTaskWeb = new RunTaskWeb();
        try {
            Runtasktable runtasktable = mgRunTaskInter.getRuntasktable(taskid);
            if (runtasktable == null) {
                return runTaskWeb;
            }
            BeanUtils.copyProperties(runtasktable,runTaskWeb);
        }
        catch (Exception e){
            logger.error("getRunTaskWeb is system error -->"+e);
        }
        return runTaskWeb;
    }

    /**
     * 保存任务
     * @param runTaskWeb 要保存的任务内容
     * @return 响应状态
     */
    public ResPonseinfo addruntask(RunTaskWeb runTaskWeb){
        if(runTaskWeb == null){
            return new ResPonseinfo(-1,"保存对象为空");
        }
        logger.info("-->>addruntask info is:"+ runTaskWeb.toString());
        try{
            Runtasktable runtasktable=new Runtasktable();
            BeanUtils.copyProperties(runTaskWeb,runtasktable);
            mgRunTaskInter.addruntask(runtasktable);
            return new ResPonseinfo(0,"OK");
        }
        catch (Exception e){
            logger.error("-->>addruntask is system error:"+e);
        }
        return new ResPonseinfo(-500,"系统异常");
    }

    /**
     * 对任务进行暂停和运行
     * @param taskid 要操作的任务ID
     * @param status 1=运行，2=暂停
     */
    public boolean goruntaskstatu(int taskid,int status){
        try{
            logger.info(String.format("-->>goruntaskstatu,taskid:%s,status:%s",taskid,status));
            mgRunTaskInter.upstatusruntask(taskid,status);
            return true;
        }
        catch (Exception e){
            logger.error("-->>upstatusruntask is system error:"+e);
        }
        return false;
    }

    /**
     * 对任务进行删除
     * @param taskid 要删除的任务ID
     */
    public boolean delruntask(int taskid){
        try{
            logger.info(String.format("-->delruntask,taskid:%s",taskid));
            mgRunTaskInter.delruntask(taskid);
            return true;
        }
        catch (Exception e){
            logger.error("-->>delruntask is system error:"+e);
            return false;
        }
    }

    public ResPonseinfo upruntask(RunTaskWeb runTaskWeb){
        if(runTaskWeb == null){
            return new ResPonseinfo(-1,"保存对象为空");
        }
        logger.info("-->>upruntask info is:"+ runTaskWeb.toString());
        try{
            Runtasktable runtasktable=new Runtasktable();
            BeanUtils.copyProperties(runTaskWeb,runtasktable);
            mgRunTaskInter.upruntask(runtasktable);
            return new ResPonseinfo(0,"OK");
        }
        catch (Exception e){
            logger.error("-->>upruntask is system error:"+e);
        }
        return new ResPonseinfo(-500,"系统异常");
    }

    public List<RunTaskWeb> getRunTaskWebforProjectid(int projectid){
        try{
            List<Runtasktable> runtasktables=mgRunTaskInter.getRuntasksforProjectid(projectid);
            if(runtasktables == null || runtasktables.isEmpty()){
                logger.info("getRuntasksforProjectid is empty;projectid:"+projectid);
                return null;
            }
            List<RunTaskWeb> runTaskWebs=new ArrayList<>();
            for(Runtasktable runtasktable:runtasktables){
                RunTaskWeb runTaskWeb=new RunTaskWeb();
                BeanUtils.copyProperties(runtasktable,runTaskWeb);
                String caseinfo=runtasktable.getCaseinfo();
                if(StringUtils.isNoneEmpty(caseinfo)){  //设置关联的用例数量
                    runTaskWeb.setCasenumber(caseinfo.split(",").length);
                }
                runTaskWebs.add(runTaskWeb);
            }
            return runTaskWebs;
        }
        catch (Exception e){
            logger.error("getRunTaskWebforProjectid is system error--->"+e);
            return null;
        }
    }


    /**
     * 查询所查项目所有的用例执行情况
     * @param projectid 要查询的项目
     * @param pagenum 页码，每页20条
     * @return 给前端展现用的
     */
    public List<RunCaseRuntWeb> getRunCaseRuntWebs(int projectid,int pagenum){
        int limits=(pagenum-1)*pagesums;
        try{
            List<Caserunt> caserunts=mgCaseRuntInter.getCaserunt(projectid,limits);
            if(caserunts == null || caserunts.isEmpty()){
                return null;
            }
            List<RunCaseRuntWeb> runCaseRuntWebs=new ArrayList<>();
            for(Caserunt caserunt:caserunts){
                RunCaseRuntWeb runCaseRuntWeb=new RunCaseRuntWeb();
                BeanUtils.copyProperties(caserunt,runCaseRuntWeb);
                runCaseRuntWebs.add(runCaseRuntWeb);
            }
            return runCaseRuntWebs;
        }
        catch (Exception e){
            logger.error("getCaserunt is system error --->"+e);
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 通过运行结果的用例ID，查询该用例关联的接口
     * @param runcaseid 要查询执行的用例ID
     * @return 该用例关联的接口的执行情况
     */
    public List<RunInterRuntWeb> getRunInterRuntWebs(int runcaseid){
        try{
            List<Interunt> interunts= mgInteruntInter.getInterunt(runcaseid);
            if(interunts == null || interunts.isEmpty()){
                return null;
            }
            List<RunInterRuntWeb> runInterRuntWebs=new ArrayList<>();
            for(Interunt interunt:interunts){
                RunInterRuntWeb runtweb=new RunInterRuntWeb();
                runtweb.setRuninterid(interunt.getId());
                runtweb.setInterfacename(interunt.getInterfacename());
                runtweb.setRunstatus(interunt.getStatus());
                runtweb.setCreatetime(interunt.getCreatetime());
                runInterRuntWebs.add(runtweb);
            }
            return runInterRuntWebs;
        }
        catch (Exception e){
            logger.error("getRunInterRuntWebs is system error --->"+e);
            e.printStackTrace();
            return null;
        }
    }

    public InterruntWeb getInterruntWeb(int runtid){
        try{
            Interunt interunt=mgInteruntInter.getInteruntforid(runtid);
            if(interunt == null){
                return null;
            }
            InterruntWeb interruntWeb=new InterruntWeb();
            BeanUtils.copyProperties(interunt,interruntWeb);

            String checkinfostr=interunt.getAutoassinfo();  //主要是处理给前端用的。将校验值转换为对象
            if(StringUtils.isNoneEmpty(checkinfostr)){
                List<CheckInfoWeb> checkInfoWebList= JSON.parseObject(checkinfostr,new TypeReference<List<CheckInfoWeb>>(){});
                interruntWeb.setCheckInfoWebs(checkInfoWebList);
            }
            return interruntWeb;
        }
        catch (Exception e){
            logger.error("getInterruntWeb is system error --->"+e);
            e.printStackTrace();
            return null;
        }
    }

}
